<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-recharge"></i> 文章列表
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-row>
                    <el-col :span="5" style="display:flex;justify-content: flex-start;">
                         <el-button
                            type="primary"
                            icon="el-icon-circle-plus-outline"
                            class="handle-del mr10"
                            @click="handleEdit(null)"
                        >添加文章</el-button>
                    </el-col>
                    <el-col :span="12">
                        <el-input v-model="query.name" placeholder="文章标题" class="handle-input mr10"></el-input>
                        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                    </el-col>
                </el-row>
            </div>
            <el-table
                :data="tableData"
                border
                v-loading="loading"
                class="table"
                 id="buctable"
                ref="multipleTable"
                header-cell-class-name="table-header"
              
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" sortable width="55" align="center"></el-table-column>
                <el-table-column label="标题" prop="title" align="center"></el-table-column>
                <el-table-column label="分类" prop="cate_name" align="center"></el-table-column>
                <el-table-column label="内容" prop="content" align="center">
                     <template slot-scope="scope" >
                      
                       <el-popover
                            placement="bottom"
                            title="内容:"
                            width="800"
                            height="600"
                            trigger="click"
                        >
                            <div v-html="scope.row.content" style="height:600px;overflow-y:auto"></div>
                            <el-link type="primary" slot="reference" :underline="false">查看</el-link>
                        </el-popover>
                     
                    </template>
                </el-table-column>
                
                <el-table-column prop="created_at" label="创建时间"></el-table-column>
                  <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                         <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row)"
                        >删除</el-button>
                    </template>
                  </el-table-column>
            </el-table>
            <el-drawer
              title="操作文章"
              :before-close="handleClose"
              :visible.sync="editDrawer"
              direction="rtl"
              custom-class="demo-drawer"
              ref="drawer"
              size="50%"
              >
              <div class="demo-drawer__content">
                <el-form :model="editForm">
                  <el-form-item label="文章标题" :label-width="formLabelWidth">
                    <el-input v-model="editForm.title" autocomplete="off"></el-input>
                  </el-form-item>
                    <el-form-item label="分类" class="form-item" :label-width="formLabelWidth">
                        <el-select v-model="editForm.cate_name"  placeholder="选择分类" style="width:100%;margin-right:10px" @change="cateChange">
                            <template v-for="(item) in cateAll">
                                <el-option
                                        :key="item.id"
                                        :label="item.title"
                                        :value="item.id"

                                ></el-option>
                            </template>
                        </el-select>

                    </el-form-item>
                  <el-form-item label="内容" :label-width="formLabelWidth">
                    <editor-bar v-model="editForm.content" :isClear="true" @change="contentChange2"></editor-bar>
                  </el-form-item>
                </el-form>
                <div class="demo-drawer__footer" style="display: flex; justify-content: flex-start; position: absolute;bottom:20px;left:0;width:100%;">
                  <el-button style="margin-left:15px;" @click="cancelForm">取 消</el-button>
                  <el-button  type="primary" @click="$refs.drawer.closeDrawer()" :loading="loading">{{ loading ? '提交中 ...' : '确 定' }}</el-button>
                </div>
              </div>
            </el-drawer>
            <div class="pagination">
                <el-pagination
                    background
                     layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
 import EditorBar from '../../common/wangEnduit';
export default {
   components: { EditorBar },
  name: 'deposit',
  data () {
    return {
      query: {
        page: 1,
        pageSize: 10,
        name:'',
      },
      formLabelWidth:'80px',
      editForm:{title:'',content:'',id:'',cate_id:'',cate_name:''},
      editDrawer:false,
      loading: false,
      tableData: [],
      multipleSelection: [],
      pageTotal: 0,
      delList: [],
      idx: -1,
      id: -1,
      cateAll: [],
      //获取列表请求
      getList: function () {
        let that = this;
        this.api.post('articleList', this.query, res => {
          that.tableData = res.data.list;
          that.query.page = res.data.page;
          that.query.pageSize = res.data.pageSize;
          that.pageTotal = res.data.pageTotal;
        }, undefined, undefined, this);
      },
        delLists: function (ids, msg) {
        let that = this;
        if (ids.length < 1) {
          this.$message.error('请选择删除的数据');
          return false;
        }
        // 二次确认删除
        this.$confirm('确定要删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          that.api.delete('articleDel', { ids: ids }, res => {
            that.query.page = 1;
            that.getList();
            that.multipleSelection = [];
            that.$message.success(res.msg);
          }, undefined, undefined, this);
        }).catch(() => { });
      }
      
    };
  },
  created () {
      this.api.get('getArticleCateAll', {}, res => {
          this.cateAll = res.data.list;
      });
    this.getList();

  },
  methods: {
    // 触发搜索按钮
    handleSearch () {
      this.$set(this.query, 'page', 1);
      this.getList();
    },
    handleClose(done) {
      if (this.loading) {
        return;
      }
      this.$confirm('确定要提交表单吗？')
        .then(_ => {
          this.loading = true;
           this.api.post('articleSave', this.editForm, res => {
              this.$message.success('保存成功');
              this.editDrawer = false;
              this.loading = false;
              this.getList();
              this.editForm = [];
           })
        })
        .catch(_ => {
            this.editDrawer = false;
            this.loading = false;
        });
    },
    cancelForm() {
      this.loading = false;
      this.editDrawer = false;
      
    },
   contentChange2 (val) {
      this.editForm.content = val;
   },
   cateChange (val) {
          this.editForm.cate_id = val;
      },

    /**
     * 添加编辑
     */
    handleEdit (row) {
      if(row){
        this.editForm = row
      }else{
          this.editForm = []
      }
      this.editDrawer = true;
    },


    // 删除操作
    handleDelete ( row) {
      this.delLists([row.id], '删除成功');
    },
   
    // 分页导航
    handlePageChange (val) {
      this.$set(this.query, 'page', val);
      this.getList();
    }
  }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
